<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <style>
        * {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        #shop-car li {
            border: 1px dotted red;
            margin: 20px 0;
            padding: 20px;
        }

        .container {
            width: 400px;
            padding: 20px;
        }
    </style>

    <div class="container">
        <input type="checkbox" id="isAllCheck" /> 全选


        <ul id="shop-car">
            <!-- <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div>
            </li>
            <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div>
            </li>
            <li>
                <input type="checkbox" />
                <div>商品名称:</div>
                <img src="https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg" alt="">
                <div>商品价格:50</div>
                <div>购买数量:30</div>
                <div>商品规格: 黑</div>
                <div>是否选中：选中吧OR不选中</div>
            </li> -->
        </ul>
        <p id="totalnum">总数量：xxx</p>
        <p id="totalprice">总价格：xxx</p>
    </div>

</body>

</html>
<script>
    let shopCar=document.getElementById("shop-car")
    let isAllCheck=document.getElementById("isAllCheck")
    let totalnum=document.getElementById("totalnum")
    let totalprice=document.getElementById("totalprice")


    // 根据购物车数据，来渲染页面
    let products = [
        {
            goods_name: '小米10-1',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '500',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 10,
            goods_is_checked: true
        },
        {
            goods_name: '小米10-2',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '510',
            goods_attr: '砂石黑4GB+64GB',
            goods_num: 50,
            goods_is_checked: true
        },
        {
            goods_name: '小米10-3',
            goods_img: 'https://img12.360buyimg.com/n0/s80x80_jfs/t1/118064/27/12885/59959/5f17b7efE453f688d/5b33ac76b2aaea9b.jpg',
            goods_price: '510',
            goods_attr: '砂石黑4GB+128GB',
            goods_num: 60,
            goods_is_checked: false
        }
    ]


    let str=""
    // 把products里面的每一项都和li结合起来
    products.forEach(function(item){
          str+=`<li>
                <input type="checkbox"  ${item.goods_is_checked?"checked":""}/>
                <div>商品名称:${item.goods_name}</div>
                <img src="${item.goods_img}" alt="">
                <div>商品价格:${item.goods_price}</div>
                <div>购买数量:${item.goods_num}</div>
                <div>商品规格: ${item.goods_attr}</div>
                <div>是否选中：${item.goods_is_checked?"此项已选中":"此项没选中"}</div>
            </li>`
    })

    shopCar.innerHTML=str




    // 全选
    // 每一个商品都被选上，那么全选才被选上，只要有一个商品没被选上，那么全选就不能被选上（全真为真，一假为假）
    let flag=products.every((item)=>{
        // return item.goods_is_checked==true
        return item.goods_is_checked
    })

    // console.log(flag);
    isAllCheck.checked=flag




    // 总数量和总价格
    let  tnum=0
    let  tprice=0
    products.forEach((item)=>{
        if(item.goods_is_checked){
            // 如果被选中了，则计算此项的数量和价格
            tnum += item.goods_num
            tprice += item.goods_num*item.goods_price //价格是由单价乘以数量得到的

        }
    })

    console.log(tnum);
    console.log(tprice);

    totalnum.innerHTML=`总数量：${tnum}`
    totalprice.innerHTML=`总价格：${tprice}`







</script>